
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  poolint.push.tablename;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import dbmanager.DBManager;

import pool.bean.IdBean;
import pool.bean.Master_table_Bean;
import pool.bean.Property_details_Bean;


public class Master_table extends IntegrateSubPool //implements IntegratePool
{
	private String masterTable="master_table";
	private String property_details = "property_details";
	
	public Master_table() { }
	
	public String insert()
	{	
		System.out.println(propertyVect);	
	
		if(propertyVect.size()!=0)
		{
			request = masterTable;
			setRequestParam();
			
			if(master == null)
			{				
				IdBean idObj = new IdBean();
				idObj.setTable("master_table");
				idObj.execute();
				String id = idObj.getId();
				
				/** It will insert into M & PL of the POOLDB. **/
				master = gin.getItem(tableObj)+ "_" + id;
				Master_table_Bean mtb = new Master_table_Bean();
				mtb.setReqParam(rqp);
				mtb.setNewMasterObject(master);		
				mtb.setPropertyVector(propertyVect);
				mtb.insert();
				FLAG = mtb.getResult();
				master =gin.getId(master);	
			}
			else
			{
				Property_details_Bean pdb = new Property_details_Bean();
				pdb.setReqParam(rqp);
				pdb.setMid(master);		
				pdb.setPropertyVector(propertyVect);
				pdb.insert();
				FLAG = pdb.getResult();
			}
			
			return master;
		}
		return null;
	}
	
	/**Getting recent object values from pool table.*/
	private void generatePoolHash()
	{
		try
		{			
			ResultSet rsPoolValue = DBManager.getSelect("SELECT mid, pid, pv, vt FROM " + property_details + " WHERE mid = " + recentTableObject + " and td is null");
			generatePropertyHash(rsPoolValue);
			rsPoolValue.close();
		}
		catch (Exception e) 
		{
			System.out.println("Exception::Integration:: PoolConnection.java, while fetching Fields Values from POOL ");
			e.printStackTrace();
		}
	}
	
	public Hashtable<Object, Object> getIntegratePoolHash() 
	{
		generatePoolHash();
		return poolHash;
	}
	
	public Vector<Object> getPropertyVector()
	{		
			
		boolean FLAG = false;
		Enumeration<Object> poolIte = fieldHash.keys();
		while(poolIte.hasMoreElements())
		{
			String key = poolIte.nextElement().toString();
			String value = fieldHash.get(key).toString();
			Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
			hash.put("mid", gin.getItem(masterObj));
			hash.put("pid", key);
			hash.put("pv", value);
			propertyVect.add(hash);
			oldPropertyVect.add(hash);
			FLAG = true;
		}
		if(FLAG)
		{
			Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
			hash.put("mid", gin.getItem(masterObj));
			hash.put("pid", "type");
			hash.put("pv", gin.getItem(tableObj));
			propertyVect.add(hash);
			oldPropertyVect.add(hash);
		}
		return propertyVect;
	}	

	public void setPropertyVector(Vector<Object> vector){
		propertyVect.addAll(vector);
	}

	public String getID(Hashtable<Object,Object> hashtable) 
	{		
		
		ResultSet rsPoolValue;
		String mid;
		try 
		{
			
			String masterid2 = hashtable.get("masterid").toString();
			String key = hashtable.get("key").toString();
			String fieldname = hashtable.get("fieldname").toString();
			String TYPE = hashtable.get("TYPE").toString();
			String tablobj = hashtable.get("tablobj").toString();			
			
			rsPoolValue = DBManager.getSelect("SELECT mid, pid, pv, vt FROM property_details  WHERE mid = " + masterid2 + " and pid ="+key);
			rsPoolValue.next();
			// get the object from the property_details where id = id_var and type = tableobj then call to insert master and generatePoolHash() function
			
			if(rsPoolValue.getString("pid").equals(fieldname));
			{
				String id = rsPoolValue.getString("pv");
				if(id.equalsIgnoreCase("0"))
				{
					id = rsPoolValue.getString("vt");
					rsPoolValue.close();
					rsPoolValue = DBManager.getSelect("SELECT p.mid, p.pid, p.pv, p.vt FROM property_details p JOIN property_details p1 ON(p.mid = p1.mid)  WHERE p.pid = " + fieldname + " and p.vt ="+id + " and p1.pid ="+TYPE+" and p1.pv="+tablobj);
				}
				else
				{
					rsPoolValue.close();
					rsPoolValue = DBManager.getSelect("SELECT p.mid, p.pid, p.pv, p.vt FROM property_details p JOIN property_details p1 ON(p.mid = p1.mid)  WHERE p.pid = " + fieldname + " and p.pv ="+id + " and p1.pid ="+TYPE+" and p1.pv="+tablobj);
				}
				rsPoolValue.next();
				mid = rsPoolValue.getString("mid");
				//mid=gin.getItem(mid);
				rsPoolValue.close();
				return mid;				
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return null;
	}

	public String getFieldValue(String master,String key)
	{
		String fieldname="";
		ResultSet rsPoolValue;
		try
		{
			rsPoolValue = DBManager.getSelect("SELECT mid, pid, pv, vt FROM property_details  WHERE mid = " + master + " and pid ="+key);
			rsPoolValue.next();
			fieldname = rsPoolValue.getString("pv");
			if(fieldname.equals("0"))
				fieldname = rsPoolValue.getString("vt");
			else
				fieldname = gin.getItem(fieldname);
			rsPoolValue.close();
			return fieldname;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	
	public String delete() 
	{
		String tempStr = ""; 
		
		/*tempStr = getMaster(oldcolumnNameValue);
		
		System.out.println(" Integration::Operation::performOperation::Del:: Delete process....");
		Hashtable<Object, Object> tempVec = new Hashtable<Object, Object>();
		tempVec.put("mid", gin.getItem(tempStr));
		tempVec.put("pid", columnName);
		tempVec.put("pv", oldcolumnNameValue);
		Vector<Object> oldVector = new Vector<Object>(); 
		oldVector.add(tempVec);*/
		
		setRequestParam();
		
		/** Deleting property from Property_details. */
		String master = gin.getItem(tempStr);
		Master_table_Bean mtb = new Master_table_Bean();
		mtb.setReqParam(rqp);
		mtb.setNewMasterObject(master);
		
		/** It will Delete record from the database.*/
		System.out.println(" POOLIntegration::Push::Master_table::Delete() Delete Message : " + mtb.delete());
		//return true;
		return master;
	}

	public String update()
	{
		if(propertyVect.size()!=0)
		{
			request = masterTable;
			setRequestParam();
			
			/** It will Update into M & PL of the POOLDB. **/
			Property_details_Bean pdb = new Property_details_Bean();
			pdb.setReqParam(rqp);
			pdb.setOldPropertyVector(oldPropertyVect);
			pdb.setNewPropertyVector(propertyVect);
			pdb.update();
			FLAG = pdb.getResult();
			//return gin.getId(masterObj);
			return masterObj;
		}
		return null;
	}
	
	private void generateOldPoolHash()
	{
		try
		{			
			ResultSet rsPoolValue = DBManager.getSelect("SELECT mid, pid, pv, vt FROM " + property_details + " WHERE mid = " + recentTableObject + " and td is not null");
			generatePropertyHash(rsPoolValue);
			rsPoolValue.close();
		}
		catch (Exception e) 
		{
			System.out.println("Exception::Integration:: PoolConnection.java, while fetching Fields Values from POOL ");
			e.printStackTrace();
		}
	}
	public Hashtable<Object, Object> getIntegrateOldPropertyHash() {
		 generateOldPoolHash();
		return poolHash;
	}
	
	public Vector<Object> getIntegrateOldPropertyVector() {
		getPropertyVector();
		return oldPropertyVect;
	}

	public void setOldPropertyVector(Vector<Object> vector) {
		oldPropertyVect.addAll(vector);		
	}

	public Hashtable<Object, Object> getUniqueKeyPoolHash() {
		getFieldname(); // It will returns the field from fieldObject for the specific TableObject.
		
		for(int i = 0; i < fieldNameList.size(); i++)
		{
			String query = "SELECT pv as uniquekeyvalue, vt FROM " + property_details + " WHERE mid = " + recentTableObject + " AND pid = " + fieldNameList.get(i).toString();
			try {
				ResultSet rs = DBManager.getSelect(query);
				rs.next();
				String uniquekeyvalue = rs.getString("uniquekeyvalue");
				if(uniquekeyvalue.equals("0"))
					uniquekeyvalue = rs.getString("vt");
				else 
					uniquekeyvalue = gin.getItem(uniquekeyvalue);
				rs.close();
				//poolHash.put(gin.getItem(uniqueKeyList.get(i).toString()), uniquekeyvalue);
				poolHash.put(gin.getItem(fieldNameList.get(i).toString()), uniquekeyvalue);
				return poolHash;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return new Hashtable<Object, Object>();
	}
	
	private void generatePropertyHash(ResultSet rsPoolValue)
	{
		try
		{
			while(rsPoolValue.next())
			{
				String pid = gin.getItem(rsPoolValue.getString("pid")).toLowerCase();
				String strPvVt = rsPoolValue.getString("pv"); // It will stores the value of PV or VT.
				
				if(strPvVt.equalsIgnoreCase("0"))
				{
					strPvVt = rsPoolValue.getString("vt");
				}
				else
				{
					strPvVt = gin.getItem(strPvVt);
				}
				poolHash.put(pid, strPvVt);				
			}
			rsPoolValue.close();
			poolVector.add(poolHash);
		}
		catch (Exception e) 
		{
			System.out.println("Exception::Integration:: PoolConnection.java, while fetching Fields Values from POOL ");
			e.printStackTrace();
		}
	}
	/*
	public void getOldPropertyVector()
	{
		boolean FLAG = false;
		Enumeration<Object> poolIte = fieldHash.keys();
		while(poolIte.hasMoreElements())
		{
			String key = poolIte.nextElement().toString();
			String value = fieldHash.get(key).toString();
			Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
			hash.put("mid", gin.getItem(masterObj));
			hash.put("pid", key);
			hash.put("pv", value);
			oldPropertyVect.add(hash);
			FLAG = true;
		}
		if(FLAG)
		{
			Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
			hash.put("mid", gin.getItem(masterObj));
			hash.put("pid", "type");
			hash.put("pv", gin.getItem(tableObj));
			oldPropertyVect.add(hash);
		}
	}*/
}
